package com.weixin.paleituo.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.weixin.paleituo.R;
import com.weixin.paleituo.adapter.VisterRecordRecycleAdapter;
import com.weixin.paleituo.application.BaseApplication;
import com.weixin.paleituo.base.BasePersonActivity;
import com.weixin.paleituo.bean.StatusCode;
import com.weixin.paleituo.bean.VisterRecordBean;
import com.weixin.paleituo.contans.Contans;
import com.weixin.paleituo.http.HttpUtil;
import com.weixin.paleituo.http.ProgressSubscriber;
import com.weixin.paleituo.rxjava.ApiUtils;
import com.weixin.paleituo.util.LogUtils;
import com.weixin.paleituo.util.ToastUtils;
import com.weixin.paleituo.util.UIUtils;
import com.weixin.paleituo.view.LoadingPagerHead;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import io.reactivex.Observable;

public class VisterRecordActivity extends BasePersonActivity {
    @BindView(R.id.recycle_activity_vister_record)
    RecyclerView mRecycleActivityVisterRecord;
    private LoadingPagerHead mLoadingPagerHead;
    private List<VisterRecordBean>  mVisterRecordBeanList = new ArrayList<>();;
    private VisterRecordRecycleAdapter mVisterRecordRecycleAdapter;

    public static void startActivity(Context context){
        Intent intent=new Intent(context,VisterRecordActivity.class);
        context.startActivity(intent);
    }
    @Override
    protected View getContentView(Context context) {

        mLoadingPagerHead = new LoadingPagerHead(this) {
            @Override
            protected void close() {

            }
            @Override
            protected View createSuccessView() {
                return UIUtils.inflate(VisterRecordActivity.this, R.layout.activity_vister_record);
            }

            @Override
            protected void reLoading() {

            }
        };

      //  setContentView(R.layout.activity_vister_record);
        return mLoadingPagerHead;
    }

    @Override
    protected void initData() {
        super.initData();
        //设置布局管理器
        mTvTitle.setText("看房记录");
        mVisterRecordRecycleAdapter = new VisterRecordRecycleAdapter(this,mVisterRecordBeanList);
        mRecycleActivityVisterRecord.setLayoutManager(new LinearLayoutManager(this));

        //设置Item增加、移除动画
        mRecycleActivityVisterRecord.setItemAnimator(new DefaultItemAnimator());
        mVisterRecordBeanList.clear();
        mRecycleActivityVisterRecord.setAdapter(mVisterRecordRecycleAdapter);


        final Observable<StatusCode<List<VisterRecordBean>>> visterRecord = ApiUtils.getApi().getVisterRecord(BaseApplication.getToken(), BaseApplication.sUserInfoBean.userId, BaseApplication.sUserInfoBean.username);
        HttpUtil.getInstance().toSubscribe(visterRecord, new ProgressSubscriber<List<VisterRecordBean>>(this) {
            @Override
            protected void _onNext(StatusCode<List<VisterRecordBean>> t) {
                List<VisterRecordBean> visterRecordBeans = t.getData();
                if (visterRecordBeans!=null &&visterRecordBeans.size()>0){
                    mVisterRecordBeanList.clear();
                    mVisterRecordBeanList.addAll(visterRecordBeans);
                    mVisterRecordRecycleAdapter.notifyDataSetChanged();

                    mLoadingPagerHead.showPagerView(Contans.STATE_SUCCEED);
                }else {
                    ToastUtils.makeText(t.getDetailMsg()+"");
                    mLoadingPagerHead.showPagerView(Contans.STATE_EMPTY);
                }
            }

            @Override
            protected void _onError(String message) {
                ToastUtils.makeText(message);

                mVisterRecordBeanList.clear();
                LogUtils.i(TAG,message);
                mVisterRecordRecycleAdapter.notifyDataSetChanged();
                mLoadingPagerHead.showPagerView(Contans.STATE_ERROR);

            }
        },"",lifecycleSubject,false,true);



    }
}
